home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Utility Library / Source / Q3UL_QD3DUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  6.9 KB  |  260 lines  |  [TEXT/CWIE]

  1. /*
  2.  *
  3.  * QuickDraw 3D code for a utility library.
  4.  *
  5.  * This file contains an amalgamation of code that has been useful for
  6.  * QuickDraw 3D developers.
  7.  *
  8.  * Nick Thompson, nickt@apple.com
  9.  * Send bug reports and feedback to devsupport@apple.com.
  10.  *
  11.  * ©1997 Apple Computer Inc, All Rights Reserved 
  12.  *
  13.  * Modification History:
  14.  *
  15.  */
  16.  
  17. #include "Q3ULPriv.h"
  18. #include "Q3UL_QD3DUtils.h"
  19.  
  20. #include "QD3DDrawContext.h"
  21. #include "QD3DLight.h"
  22. #include "QD3DRenderer.h"
  23.  
  24. /*------------------------------------------------------------------------------
  25.  * 
  26.  */ 
  27.  
  28. TQ3ViewObject Q3UL_NewView(TQ3UL_WindowRef theWindow)
  29. {
  30.     TQ3Status                myStatus;
  31.     TQ3ViewObject            myView;
  32.     TQ3DrawContextObject        myDrawContext;
  33.     TQ3RendererObject        myRenderer;
  34.     TQ3CameraObject            myCamera;
  35.     TQ3GroupObject            myLights;
  36.     
  37.     myView = Q3View_New();
  38.     
  39.     /*    Create and set draw context. */
  40.     if ((myDrawContext = Q3UL_NewMacintoshDrawContext(theWindow)) == nil )
  41.         goto bail;
  42.         
  43.     if ((myStatus = Q3View_SetDrawContext(myView, myDrawContext)) == kQ3Failure )
  44.         goto bail;
  45.  
  46.     Q3Object_Dispose( myDrawContext ) ;
  47.     
  48.     /*    Create and set renderer. */
  49.     
  50.     
  51.     
  52. #if 0
  53.     /* this would use the wireframe renderer */
  54.     myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireFrame);
  55.     if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure ) {
  56.         goto bail;
  57.     }
  58.     
  59. #else
  60.  
  61.     /* this would use the interactive software renderer */
  62.     if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != nil ) {
  63.         if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure ) {
  64.             goto bail;
  65.         }
  66.     }
  67.     else {
  68.         goto bail;
  69.     }
  70. #endif
  71.  
  72.     Q3Object_Dispose( myRenderer ) ;
  73.     
  74.     /*    Create and set camera. */
  75.     if ( (myCamera = Q3UL_NewCamera(theWindow)) == nil )
  76.         goto bail;
  77.         
  78.     if ((myStatus = Q3View_SetCamera(myView, myCamera)) == kQ3Failure )
  79.         goto bail;
  80.  
  81.     Q3Object_Dispose( myCamera ) ;
  82.     
  83.     /*    Create and set lights. */
  84.     if ((myLights = Q3UL_NewDefaultLightGroup()) == nil )
  85.         goto bail;
  86.         
  87.     if ((myStatus = Q3View_SetLightGroup(myView, myLights)) == kQ3Failure )
  88.         goto bail;
  89.         
  90.     Q3Object_Dispose(myLights);
  91.  
  92.     return ( myView );
  93.     
  94. bail:
  95.     /*    If any of the above failed, then don't return a view. */
  96.     return ( nil );
  97. }
  98.  
  99. /*------------------------------------------------------------------------------
  100.  * 
  101.  */ 
  102.  
  103.  
  104. TQ3DrawContextObject Q3UL_NewMacintoshDrawContext(TQ3UL_WindowRef theWindow)
  105. {
  106.     TQ3DrawContextData        myDrawContextData;
  107.     TQ3MacDrawContextData    myMacDrawContextData;
  108.     TQ3ColorARGB            ClearColor;
  109.     WindowPtr                theMacWindow ;
  110.     TQ3DrawContextObject    myDrawContext ;
  111.     
  112.     /*    Set the background color. */
  113.     ClearColor.a = 1.0;
  114.     ClearColor.r = 1.0;
  115.     ClearColor.g = 1.0;
  116.     ClearColor.b = 1.0;
  117.     
  118.     /*    Fill in draw context data. */
  119.     myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor;
  120.     myDrawContextData.clearImageColor = ClearColor;
  121.     myDrawContextData.paneState = kQ3False;
  122.     myDrawContextData.maskState = kQ3False;
  123.     myDrawContextData.doubleBufferState = kQ3True;
  124.  
  125.     myMacDrawContextData.drawContextData = myDrawContextData;
  126.     
  127.     /* this is the window associated with the view */
  128.     theMacWindow = gWindowArray[theWindow].window ;
  129.         
  130.     myMacDrawContextData.window = (CGrafPtr) theMacWindow;        
  131.     myMacDrawContextData.library = kQ3Mac2DLibraryNone;
  132.     myMacDrawContextData.viewPort = nil;
  133.     myMacDrawContextData.grafPort = nil;
  134.     
  135.     /*    Create draw context and return it, if it’s nil the caller must handle */
  136.     myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData) ;
  137.  
  138.     return myDrawContext ;
  139. }
  140.  
  141. /*------------------------------------------------------------------------------
  142.  * 
  143.  */ 
  144.  
  145.  
  146. TQ3CameraObject Q3UL_NewCamera(TQ3UL_WindowRef theWindow)
  147. {
  148.     TQ3ViewAngleAspectCameraData    perspectiveData;
  149.     TQ3CameraObject                camera;
  150.     
  151.     TQ3Point3D                     from     = { 0.0, 0.0, 7.0 };
  152.     TQ3Point3D                     to         = { 0.0, 0.0, 0.0 };
  153.     TQ3Vector3D                 up         = { 0.0, 1.0, 0.0 };
  154.  
  155.     float                         fieldOfView = 1.0;
  156.     float                         hither         = 0.001;
  157.     float                         yon         = 1000;
  158.     
  159.     TQ3Status                    returnVal = kQ3Failure ;
  160.     WindowPtr                    theMacWindow ;
  161.  
  162.     perspectiveData.cameraData.placement.cameraLocation     = from;
  163.     perspectiveData.cameraData.placement.pointOfInterest     = to;
  164.     perspectiveData.cameraData.placement.upVector             = up;
  165.  
  166.     perspectiveData.cameraData.range.hither    = hither;
  167.     perspectiveData.cameraData.range.yon     = yon;
  168.  
  169.     perspectiveData.cameraData.viewPort.origin.x = -1.0;
  170.     perspectiveData.cameraData.viewPort.origin.y = 1.0;
  171.     perspectiveData.cameraData.viewPort.width = 2.0;
  172.     perspectiveData.cameraData.viewPort.height = 2.0;
  173.     
  174.     perspectiveData.fov                = fieldOfView;
  175.     
  176.     theMacWindow = gWindowArray[theWindow].window ;
  177.  
  178.     perspectiveData.aspectRatioXToY    =
  179.         (float) (theMacWindow->portRect.right - theMacWindow->portRect.left) / 
  180.         (float) (theMacWindow->portRect.bottom - theMacWindow->portRect.top);
  181.         
  182.     camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
  183.  
  184.     return camera ;
  185. }
  186.  
  187.  
  188. /*------------------------------------------------------------------------------
  189.  * 
  190.  */ 
  191.  
  192. TQ3GroupObject Q3UL_NewDefaultLightGroup( void )
  193. {
  194.     TQ3GroupPosition        myGroupPosition;
  195.     TQ3GroupObject            myLightList;
  196.     TQ3LightData            myLightData;
  197.     TQ3PointLightData        myPointLightData;
  198.     TQ3DirectionalLightData    myDirectionalLightData;
  199.     TQ3LightObject            myAmbientLight, myPointLight, myFillLight;
  200.     TQ3Point3D                pointLocation = { -10.0, 0.0, 10.0 };
  201.     TQ3Vector3D                fillDirection = { 10.0, 0.0, 10.0 };
  202.     TQ3ColorRGB                WhiteLight = { 1.0, 1.0, 1.0 };
  203.     
  204.     /*    Set up light data for ambient light.  This light data will be used for 
  205.      * point and fill light also.
  206.      */
  207.  
  208.     myLightData.isOn = kQ3True;
  209.     myLightData.color = WhiteLight;
  210.     
  211.     /*    Create ambient light. */
  212.     myLightData.brightness = .2;
  213.     myAmbientLight = Q3AmbientLight_New(&myLightData);
  214.     if ( myAmbientLight == nil )
  215.         goto bail;
  216.     
  217.     /*    Create point light. */
  218.     myLightData.brightness = 1.0;
  219.     myPointLightData.lightData = myLightData;
  220.     myPointLightData.castsShadows = kQ3False;
  221.     myPointLightData.attenuation = kQ3AttenuationTypeNone;
  222.     myPointLightData.location = pointLocation;
  223.     myPointLight = Q3PointLight_New(&myPointLightData);
  224.     if ( myPointLight == nil )
  225.         goto bail;
  226.  
  227.     /*    Create fill light. */
  228.     myLightData.brightness = .2;
  229.     myDirectionalLightData.lightData = myLightData;
  230.     myDirectionalLightData.castsShadows = kQ3False;
  231.     myDirectionalLightData.direction = fillDirection;
  232.     myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);
  233.     if ( myFillLight == nil )
  234.         goto bail;
  235.  
  236.     /*    Create light group and add each of the lights into the group. */
  237.     myLightList = Q3LightGroup_New();
  238.     if ( myLightList == nil )
  239.         goto bail;
  240.     myGroupPosition = Q3Group_AddObject(myLightList, myAmbientLight);
  241.     if ( myGroupPosition == 0 )
  242.         goto bail;
  243.     myGroupPosition = Q3Group_AddObject(myLightList, myPointLight);
  244.     if ( myGroupPosition == 0 )
  245.         goto bail;
  246.     myGroupPosition = Q3Group_AddObject(myLightList, myFillLight);
  247.     if ( myGroupPosition == 0 )
  248.         goto bail;
  249.  
  250.     Q3Object_Dispose( myAmbientLight ) ;
  251.     Q3Object_Dispose( myPointLight ) ;
  252.     Q3Object_Dispose( myFillLight ) ;
  253.  
  254.     return ( myLightList );
  255.     
  256. bail:
  257.     /* If any of the above failed, then return nothing */
  258.     return ( nil );
  259. }
  260.